package music_store.ms.controller.backend;

import music_store.ms.common.Const;
import music_store.ms.common.ResponseCode;
import music_store.ms.common.ServerResponse;
import music_store.ms.pojo.User;
import music_store.ms.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/manage/user/")
public class UserManageController {

    @Autowired
    IUserService iUserService;

    @GetMapping("list_user.do")
    public ServerResponse listUser(HttpSession session,
                                   @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                   @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        if (checkAdminRole(session).isSuccess()){
            return iUserService.listUser(pageNum, pageSize);
        }
        return ServerResponse.createByErrorMessage("不是管理员，无权限操作。");
    }



    //检测当前用户是否为管理员的私有方法
    private ServerResponse checkAdminRole (HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (user.getRole() == Const.Role.ROLE_ADMIN){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorCodeMessage(1,"不是管理员。");
    }


}
